package com.kim.store.service.impl;

import com.kim.store.entity.Cart;
import com.kim.store.entity.Product;
import com.kim.store.mapper.CartMapper;
import com.kim.store.mapper.ProductMapper;
import com.kim.store.service.ICartService;
import com.kim.store.service.ex.AccessDeniedException;
import com.kim.store.service.ex.CartNotFoundException;
import com.kim.store.service.ex.InsertException;
import com.kim.store.service.ex.UpdateException;
import com.kim.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    /**
     * 购物车的业务层依赖于购物车的持久层以及商品的持久层
     */
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer uid, Integer pid,
                          Integer amount, String username) {
        //查询当前要添加的购物车是否在表中存在
        Cart res = cartMapper.findByUidAndPid(uid, pid);
        if(res == null){//表示商品从来没被添加到购物车中，则新增操作
            Cart cart = new Cart();
            //补全数据:参数传递的数据
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            //补全价格：来自于商品中的数据
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());
            cart.setCreatedUser(username);
            cart.setCreatedTime(new Date());
            cart.setModifiedTime(new Date());
            cart.setModifiedUser(username);
            Integer row = cartMapper.insert(cart);
            if(row!=1){
                throw new InsertException("插入数据时产生未知异常");
            }
        }else {//表示当前商品在购物车中已经存在，则更新这条数据的num值
            Integer cid = res.getCid();
            Integer num = res.getNum()+amount;
            Integer row = cartMapper.updateNumByCid(cid, num, username, new Date());
            if(row!=1){
                throw new UpdateException("更新数据时产生未知异常");
            }
        }
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        List<CartVO> list = cartMapper.findVOByUid(uid);
        return list;
    }

    @Override
    public Integer addNum(Integer cid, Integer uid,String username) {
        Cart res = cartMapper.findByCid(cid);
        if(res == null){
            throw new CartNotFoundException("购物车数据不存在");
        }
        if(!(res.getUid().equals(uid))){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = res.getNum()+ 1;
        Integer row = cartMapper.updateNumByCid(cid, num, username, new Date());
        if(row!=1){
            throw new UpdateException("更新数据失败");
        }
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart res = cartMapper.findByCid(cid);
        if(res == null){
            throw new CartNotFoundException("购物车数据不存在");
        }
        if(!(res.getUid().equals(uid))){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = res.getNum() - 1;
        Integer row = cartMapper.updateNumByCid(cid, num, username, new Date());
        if(row!=1){
            throw new UpdateException("更新数据失败");
        }
        return num;
    }

    @Override
    public List<CartVO> getVOByCid(Integer[] cids, Integer uid) {
        List<CartVO> list = cartMapper.findVOByCid(cids);
        //迭代器
        Iterator<CartVO> iterator = list.iterator();
        while (iterator.hasNext()){
            CartVO cartVO = iterator.next();
            if(!(cartVO.getUid().equals(uid))){ //表示当前数据不属于当前用户
                //从集合中移除这个元素
                list.remove(cartVO);
            }
        }
        return list;
    }
}
